﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Text;
using System.Windows.Forms;
using SasPlugIn.DB;

namespace SasPlugIn.Table
{
    public partial class DlgStarSchemeParam : Form
    {

        public DlgStarSchemeParam(StarSchemeParameters schemeParameters)
        {
            InitializeComponent();
            propertyGridBook.SelectedObject = schemeParameters;
            
        }

        private void buttonSave_Click(object sender, EventArgs e)
        {
            this.DialogResult = System.Windows.Forms.DialogResult.OK;
        }

        private void buttonHeaderOrder_Click(object sender, EventArgs e)
        {
            OpenFileDialog openDialog = new OpenFileDialog();
            try
            {
                openDialog.Filter = "csv files|*.csv|All Files|*.*";
                if (openDialog.ShowDialog() != DialogResult.OK) return;
                {
                    using (StreamReader sr = new StreamReader(openDialog.FileName, Encoding.Default))
                    {
                        string head = sr.ReadLine();
                        StarSchemeParameters schemeParameters = (StarSchemeParameters) this.propertyGridBook.SelectedObject;
                        char sep = schemeParameters.CsvSeparator;
                        string[] arr = head.Split(sep);
                        schemeParameters.HeaderOrder.Clear();
                        foreach (string h in arr)
                        {
                            schemeParameters.HeaderOrder.Add(new CsvHeaderColumn(h));
                        }
                        propertyGridBook.SelectedObject = schemeParameters;

                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("Failed to load config file: \n" + ex.Message);
            }

        }
    }
}
